<?php

namespace app\admin\controller;


use app\common\controller\CommonController;
use app\common\service\AuthService;
use app\common\tools\Data;
use think\Db;

class Menu extends CommonController
{
    protected $table = 'SystemMenu';

    public function index()
    {
        $this->title = '系统菜单管理';
        $list = Db::name('SystemMenu')->select();

        $list = $this->_index_page_filter($list);
        $this->assign('list',$list);
        $this->assign('title',$this->title);
        return $this->fetch();
    }
    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _index_page_filter(&$data)
    {

        foreach ($data as &$vo) {
            if ($vo['url'] !== '#') {
                $vo['url'] = url($vo['url']) . (empty($vo['params']) ? '' : "?{$vo['params']}");
            }
            $vo['ids'] = join(',', Data::getArrSubIds($data, $vo['id']));
        }
        $data = Data::arr2table($data);
        return $data;
    }
    /**
     * 添加系统菜单
     */
    public function add()
    {
        if($this->request->isPost()){
            $insertData['pid'] = input('post.pid');
            $insertData['title'] = input('post.title');
            $insertData['params'] = input('post.params');
            $res = Db::name($this->table)->insert($insertData);
            if($res){
                $this->_success('添加菜单成功');
            }else{
                $this->_error('添加菜单失败');
            }
        }
        $pid = input('get.pid');
        if($pid){
            $this->_form_filter($pid);
        }else{
            $this->_form_filter('');
        }

        $this->assign('menus',$this->menus);
        $this->assign('nodes',$this->nodes);
        return $this->fetch('form');
    }

    /**
     * 表单数据处理
     * @param array $pid
     * @throws \ReflectionException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _form_filter($pid)
    {

        if ($this->request->isGet()) {
            // 上级菜单处理
            $_menus = Db::name($this->table)->where(['status' => '1'])->order('sort asc,id asc')->select();
            $_menus[] = ['title' => '顶级菜单', 'id' => '0', 'pid' => '-1'];
            $menus = Data::arr2table($_menus);
            foreach ($menus as $key => &$menu) if (substr_count($menu['path'], '-') > 3) unset($menus[$key]); # 移除三级以下的菜单
            elseif (isset($pid) && $pid !== '' && $cur = "-{$pid}-")
                if (stripos("{$menu['path']}-", "{$cur}-") !== false || $menu['path'] === $cur) unset($menus[$key]); # 移除与自己相关联的菜单
            // 选择自己的上级菜单
            if (!isset($pid) && $this->request->get('pid', '0')) $pid = $this->request->get('pid', '0');
            // 读取系统功能节点
            $nodes = AuthService::get();
            foreach ($nodes as $key => $node) {
                if (empty($node['is_menu'])) unset($nodes[$key]);
                unset($nodes[$key]['pnode'], $nodes[$key]['is_login'], $nodes[$key]['is_menu'], $nodes[$key]['is_auth']);
            }
            list($this->menus, $this->nodes) = [$menus, array_values($nodes)];
        }
    }

    /**
     * 删除
     */
    public function remove()
    {
        $id = input('post.id');
        $res = Db::name($this->table)->delete($id);
        if($res){
            $this->_success('删除菜单成功');
        }else{
            $this->_error('删除菜单失败');
        }

    }

    /**
     * 启用系统菜单
     */
    public function resume()
    {
        $id = input('post.id');
        $res = Db::name($this->table)->where(['id'=>$id])->update(['status'=>1]);
        if($res){
            $this->_success('启用菜单成功');
        }else{
            $this->_error('启用菜单失败');
        }
    }

    /**
     * 禁用系统菜单
     */
    public function forbid()
    {
        $id = input('post.id');
        $res = Db::name($this->table)->where(['id'=>$id])->update(['status'=>0]);
        if($res){
            $this->_success('禁用菜单成功');
        }else{
            $this->_error('禁用菜单失败');
        }
    }

    /**
     * 编辑系统菜单
     */

    public function edit()
    {
        $id = input('get.id');
        $find = Db::name($this->table)->where(['id'=>$id])->find();
        $this->assign('vo',$find);
        $this->_form_filter('');
        $this->assign('menus',$this->menus);
        $this->assign('nodes',$this->nodes);
        return $this->fetch('form');
    }

    


}